package com.ruoyi.project.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysLuck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysBankMapper;
import com.ruoyi.project.system.domain.SysBank;
import com.ruoyi.project.system.service.ISysBankService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 银行人员Service业务层处理
 *
 * @author theme
 * @date 2021-04-20
 */
@Service
public class SysBankServiceImpl implements ISysBankService
{
    @Autowired
    private SysBankMapper sysBankMapper;

    /**
     * 查询银行人员
     *
     * @param bankUserId 银行人员ID
     * @return 银行人员
     */
    @Override
    public SysBank selectSysBankById(Long bankUserId)
    {
        return sysBankMapper.selectSysBankById(bankUserId);
    }

    /**
     * 查询银行人员列表
     *
     * @param sysBank 银行人员
     * @return 银行人员
     */
    @Override
    public List<SysBank> selectSysBankList(SysBank sysBank)
    {
        return sysBankMapper.selectSysBankList(sysBank);
    }

    /**
     * 新增银行人员
     *
     * @param sysBank 银行人员
     * @return 结果
     */
    @Override
    public int insertSysBank(SysBank sysBank)
    {
        sysBank.setCreateTime(DateUtils.getNowDate());
        return sysBankMapper.insertSysBank(sysBank);
    }

    /**
     * 修改银行人员
     *
     * @param sysBank 银行人员
     * @return 结果
     */
    @Override
    public int updateSysBank(SysBank sysBank)
    {
        sysBank.setUpdateTime(DateUtils.getNowDate());
        return sysBankMapper.updateSysBank(sysBank);
    }

    /**
     * 批量删除银行人员
     *
     * @param bankUserIds 需要删除的银行人员ID
     * @return 结果
     */
    @Override
    public int deleteSysBankByIds(Long[] bankUserIds)
    {
        return sysBankMapper.deleteSysBankByIds(bankUserIds);
    }

    /**
     * 删除银行人员信息
     *
     * @param bankUserId 银行人员ID
     * @return 结果
     */
    @Override
    public int deleteSysBankById(Long bankUserId)
    {
        return sysBankMapper.deleteSysBankById(bankUserId);
    }

    @Override
    @Transactional
    public String importBank(List<SysBank> list,String areaName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new BaseException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysBank bank : list) {
            if(StringUtils.isEmpty(bank.getUserName())&&StringUtils.isEmpty(bank.getPhone())&&StringUtils.isEmpty(bank.getBranch())&&StringUtils.isEmpty(bank.getOutlets())){
                break;
            }
            try {
                // 验证是否存在这个用户
                SysBank u = sysBankMapper.selectBankByPhone(bank.getPhone());
                if (null == u&&StringUtils.isNotEmpty(bank.getPhone())&&StringUtils.isNotEmpty(bank.getUserName())&&bank.getPhone().length()==11) {
                    bank.setAreaName(areaName);
                    this.insertSysBank(bank);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、手机号 " + bank.getPhone() + " 新增成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、手机号 " + bank.getPhone() + " 已存在或数据格式错误");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、手机号 " + bank.getPhone() + " 新增失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，新增失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public SysBank selectBankByPhone(String phone) {
        return sysBankMapper.selectBankByPhone(phone);
    }

    @Override
    public SysBank selectBankByPhoneAndId(String phone, Long companyId) {
        return sysBankMapper.selectBankByPhoneAndId(phone,companyId);
    }

    public static void main(String[] args) {
        System.out.println("18252578446".length());
    }
}
